package com.controller.merchant;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.dto.merchant.PermissionDTO;
import com.dto.merchant.RoleDTO;
import com.model.dao.PermissionTable;
import com.model.dao.PermissionTableDAO;
import com.model.dao.RolePermissionTable;
import com.model.dao.RoleTable;
import com.model.dao.RoleTableDAO;

@ManagedBean(name = "roleController")
@SessionScoped
public class RoleController implements Serializable{

	private static final long serialVersionUID = -3032247825121158765L;

	public List<RoleDTO> findAll(){
		RoleTableDAO dao =new RoleTableDAO();
		List<RoleTable> list=dao.findAll();
		List<RoleDTO> roleList=new ArrayList<RoleDTO>();
		for(RoleTable table:list){
			RoleDTO roleDTO=new RoleDTO();
			roleDTO.setActive((table.getActive().equalsIgnoreCase("Y")?true:false));
			roleDTO.setRoleDescription(table.getRoleDescription());
			roleDTO.setRoleId(table.getIdroleTable());
			roleDTO.setRoleName(table.getRoleName());
			List<PermissionDTO> permissionList=new ArrayList<PermissionDTO>();
			for(RolePermissionTable permTable: table.getRolePermissionTables()){
				PermissionDTO permissionDTO=new PermissionDTO();
				permissionDTO.setActive(permTable.getPermissionTable().getActive().equalsIgnoreCase("Y")?true:false);
				permissionDTO.setPermissionDescription(permTable.getPermissionTable().getPermissionDescription());
				permissionDTO.setPermissionName(permTable.getPermissionTable().getPermissionName());
				permissionDTO.setPermissionId(permTable.getPermissionTable().getIdpermissionTable());
				permissionList.add(permissionDTO);
			}
			roleDTO.setPermissionList(permissionList);
			roleList.add(roleDTO);
		}
		System.out.println("role list size"+roleList.size());
		return roleList;
	}
	
	public void addRole(RoleDTO dto){
		RoleTableDAO dao=new RoleTableDAO();
		RoleTable table=new RoleTable();
		table.setActive(dto.isActive()?"Y":"N");
		table.setRoleDescription(dto.getRoleDescription());
		table.setRoleName(dto.getRoleName());
		Set<RolePermissionTable> rolePermissionTables = new HashSet<RolePermissionTable>(0);
		for(PermissionDTO permissionDTO:dto.getPermissionList()){
		RolePermissionTable rolePermissionTable=new RolePermissionTable();
		rolePermissionTable.setPermissionTable(new PermissionTable(permissionDTO.getPermissionId(),permissionDTO.getPermissionName()));
		rolePermissionTable.setRoleTable(table);
		rolePermissionTables.add(rolePermissionTable);
		}
		table.setRolePermissionTables(rolePermissionTables);
		dao.persist(table);
	}
	public void editRole(RoleDTO dto){
		RoleTableDAO dao=new RoleTableDAO();
		RoleTable table=dao.findById(dto.getRoleId());
		table.setActive(dto.isActive()?"Y":"N");
		table.setRoleDescription(dto.getRoleDescription());
		table.setRoleName(dto.getRoleName());
		Set<RolePermissionTable> rolePermissionTables = new HashSet<RolePermissionTable>(0);
		for(PermissionDTO permissionDTO:dto.getPermissionList()){
		RolePermissionTable rolePermissionTable=new RolePermissionTable();
		rolePermissionTable.setPermissionTable(new PermissionTable(permissionDTO.getPermissionId(),permissionDTO.getPermissionName()));
		rolePermissionTable.setRoleTable(table);
		rolePermissionTables.add(rolePermissionTable);
		}
		table.setRolePermissionTables(rolePermissionTables);
		dao.merge(table);
	}
	public List<PermissionDTO> findAllPermissions(){
		PermissionTableDAO dao =new PermissionTableDAO();
		List<PermissionTable> list=dao.findAll();
		List<PermissionDTO> permissionList=new ArrayList<PermissionDTO>();
		for(PermissionTable table:list){
			PermissionDTO permissionDTO=new PermissionDTO();
			permissionDTO.setActive((table.getActive().equalsIgnoreCase("Y")?true:false));
			permissionDTO.setPermissionDescription(table.getPermissionDescription());
			permissionDTO.setPermissionId(table.getIdpermissionTable());
			permissionDTO.setPermissionName(table.getPermissionName());
			permissionList.add(permissionDTO);
		}
		return permissionList;
	}
}
